do "E:\yungyu\lottery_baby\do\set_environment.do"

forv i = 93(1)110{
	use "$egx\egxt615_`i'.dta", clear
	gen shr = shr_nmrt / shr_dnt
	keep idn_ban_tp idn_ban hsn_tp hou_losn shr

	rename idn_ban idn
	rename hsn_tp hou_hsn_cd
	gen hou_town_cd = substr(hou_losn,1,2) 
	gen hou_vill_cd = substr(hou_losn,3,2) 
	gen year = `i'
	
	order year idn_ban_tp idn hou_losn hou_hsn_cd hou_town_cd hou_vill_cd shr
	
	rename hou_hsn_cd res_hsn_cd  
	rename hou_town_cd res_town_cd
	rename hou_vill_cd res_vill_cd
	
	merge m:1 res_hsn_cd res_town_cd res_vill_cd using "$wdata/top1_prob_village.dta"
	drop if _m == 2
	drop _m
	foreach x in top1 top5 top10 top20{
		recode `x' . = 0
		rename `x' prob_`x'_vill
	}
	
	merge m:1 res_hsn_cd res_town_cd using "$wdata/top1_prob_township.dta"
	drop if _m == 2
	drop _m
	foreach x in top1 top5 top10 top20{
		recode `x' . = 0
		rename `x' prob_`x'_town
	}
	
	rename res_hsn_cd hou_hsn_cd   
	rename res_town_cd hou_town_cd 
	rename res_vill_cd hou_vill_cd 
	
	save "$wdata/hou_location_`i'.dta", replace

	collapse (max)prob*, by(idn year)
	
	save "$wdata/hou_max_prop_`i'.dta", replace
}
